package com.siqi.hr.system.common.sql;

import com.siqi.hr.system.common.chan.BeSql;
import org.apache.commons.lang.StringUtils;

/**
 * 拼sql的辅助类
 */
public class SField implements BeSql {
	public static final String PK_FIELD_BEGIN_WITH = "PK";
	//分割条件
	private String[] separations;
	//字段名
	private String name;
	//表名
	private STable table;
	//字段别名
	private String byName;
	//添加字段可点击功能
	private String fuction;

	public SField(String name, STable table) {
		this.name = name;
		this.table = table;
		this.separations = name.split("_");
	}

	public SField(String name, STable table, String byName) {
		this.name = name;
		this.table = table;
		this.separations = name.split("_");
		this.byName = byName;
	}

	public String getFieldName() {
		return this.separations[0];
	}

	public String getFuction() {
		return this.fuction;
	}

	public void setFuction(String fuction) {
		this.fuction = fuction;
	}

	public String getByName() {
		return this.byName;
	}

	public void setByName(String byName) {
		this.byName = byName;
	}

	public boolean isQuote() {
		return "PK".equals(getFieldName().toUpperCase());
	}
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public STable getTable() {
		return this.table;
	}

	public void setTable(STable table) {
		this.table = table;
	}

	/**
	 * 获取字段类型
	 * @return
     */
	public SFieldType getSFieldType() {
		if (this.name.equals("id"))
			return FieldTypeParse.newInstance().getSFieldType("varchar");
		if (this.name.equals("name")) {
			return FieldTypeParse.newInstance().getSFieldType("varchar");
		}
		//如果添加的是引用字段
		if (getFieldName().toUpperCase().equals("PK")) {
			return FieldTypeParse.newInstance().getSFieldType("varchar");
		}
		//如果添加的是身份证号、银行卡号
		if (this.name.endsWith("_idcard") || this.name.endsWith("_bankcard")) {
			return FieldTypeParse.newInstance().getSFieldType("varchar");
		}
		if (this.name.indexOf("_") == -1) {
			return FieldTypeParse.newInstance().getSFieldType("varchar");
		}
		return FieldTypeParse.newInstance().getSFieldType(this.separations[(this.separations.length - 1)]);
	}

	public StringBuilder toSqlString() {
		StringBuilder sb = null;
		sb = this.table == null ? new StringBuilder(this.name) : new StringBuilder(this.table.getByName() + "." + this.name);
		sb = addFunciton(sb);
		if (getByName() != null) {
			sb.append(" as " + getByName());
		}
		return sb;
	}

	private StringBuilder addFunciton(StringBuilder sb) {
		if (this.fuction == null)
			return sb;
		String replacement = sb.toString();
		return new StringBuilder(StringUtils.replace(this.fuction, "@",replacement));
	}

	public String toString() {
		return this.name;
	}

    /**
	 * 判断字段是否是引用字段(也就是是否是pk开头)
	 * @return
     */
	public Boolean isQuoteCustomTable() {
		if ((StringUtils.startsWithIgnoreCase(getName(), "PK")) && (StringUtils.split(getName(), "_")[1].equals("t")) && (StringUtils.split(getName(), "_")[2].matches("[0-9]*")))
			return Boolean.valueOf(true);
		return Boolean.valueOf(false);
	}

	public STable getQuoteTable() {
		if (!isQuote())
			throw new RuntimeException("不是引用字段，不能获取引用对象" + getName());
		return new STable(this.name.substring(3, this.name.lastIndexOf('_')));
	}

	public static boolean isSelectBox(String tname) {
		boolean radio = tname.indexOf("_radio") != -1;
		boolean singlebox = tname.indexOf("_singlebox") != -1;
		boolean mutibox = tname.indexOf("_mutibox") != -1;
		boolean r = (radio) || (singlebox) || (mutibox);
		return r;
	}

	public boolean equals(Object obj) {
		if ((obj != null) && ((obj instanceof SField)) && (this.name != null)
				&& (this.name.equals(((SField) obj).getName()))
				&& (this.table != null)
				&& (this.table.equals(((SField) obj).getTable()))) {
			return true;
		}
		return false;
	}

	public int hashCode() {
		return (this.table.getName() + "." + this.name).hashCode();
	}

	public static enum Function {
		IF("adf"), IFNULL("adf");

		private String str;

		private Function(String s) {
			this.str = s;
		}

		public String get() {
			return this.str;
		}
	}
}
